import { getSupplierMaterialList } from '../../../api/index'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    selectIds: {
      type: Array,
      value: []
    }
  },
  data: {
    currentPage: 1,
    pageSize: 15,
    list: [],
    isEmpty: false,
    sections: []
  },
  methods: {
    search ({ detail }) {
      this.setData({ keyword: detail })
      this.getList()
    },
    close () {
      this.triggerEvent('close')
    },
    toggleCheck ({ currentTarget: { dataset: { item, index } } }) {
      let key = `list[${index}].isCheck`
      this.setData({ [key]: !item.isCheck })
      this.setSections()
    },
    cancelCheck () {
      this.data.list.forEach(item => {
        item.isCheck = false
      })
      this.setData({ list: this.data.list })
      this.setSections()
    },
    setSections () {
      let sections = this.data.list.filter(item => item.isCheck)
      this.setData({ sections: sections })
    },
    add () {
      if (!this.data.sections.length) {
        wx.showToast({ title: '请选择要添加的材料！', icon: 'none' })
        return
      }
      this.triggerEvent('confirm', this.data.sections)
    },
    async getList () {
      try {
        let query = {
          currentPage: 1,
          pageSize: this.data.pageSize,
          isOnSales: true
        }
        if (this.data.keyword) query.searchContent = this.data.keyword
        if (this.data.selectIds.length) query.materialList = this.data.selectIds.join(',')
        const { ResultSet } = await getSupplierMaterialList({ ...query })
        this.loading = false
        this.alloaded = false
        if (ResultSet.userData.data.length) {
          this.data.currentPage = 2
          ResultSet.userData.data.forEach(item => {
            item.isCheck = false
          })
        }
        this.data.isEmpty = ResultSet.userData.data.length && ResultSet.userData.data.length < this.data.pageSize
        this.setData({
          list: ResultSet.userData.data,
          currentPage: this.data.currentPage,
          isEmpty: this.data.isEmpty
        })
        this.loading = false
      } catch (err) {
        this.loading = false
        wx.showToast({ title: err.message || err.ResultSet.errors })
      }
    },
    async loadMore () {
      try {
        if (this.loading || this.alloaded) return
        this.loading = true
        let query = {
          currentPage: this.data.currentPage,
          pageSize: this.data.pageSize,
          isOnSales: true
        }
        if (this.data.keyword) query.searchContent = this.data.keyword
        if (this.data.selectIds.length) query.materialList = this.data.selectIds.join(',')
        const { ResultSet } = await getSupplierMaterialList({ ...query })
        this.alloaded = !ResultSet.userData.data.length
        this.data.isEmpty = !ResultSet.userData.data.length
        this.setData({ isEmpty: this.data.isEmpty })
        if (ResultSet.userData.data.length) {
          ResultSet.userData.data.forEach(item => {
            item.isCheck = false
          })
          this.data.currentPage = this.data.currentPage + 1
          this.data.list.push(...ResultSet.userData.data)
          this.setData({
            list: this.data.list,
            currentPage: this.data.currentPage
          })
        }
        this.loading = false
      } catch (err) {
        console.log(err)
        this.loading = false
        wx.showToast({ title: err.message || err.ResultSet.errors })
      }
    },
    beforeOpen () {
      this.getList()
    }
  },
})